{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "6910f596-e93b-48b7-96aa-679de99af280",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "10\n",
      "20\n",
      "30\n"
     ]
    }
   ],
   "source": [
    "for i in [10, 20, 30]:\n",
    "    print(i)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "33b1825c-fd8d-46c6-9e2c-6f7d369115f4",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "a\n",
      "b\n",
      "c\n"
     ]
    }
   ],
   "source": [
    "for i in 'abc':\n",
    "    print(i)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "4bd6a87b-a3af-4d7a-82fa-3b03ad7bceb4",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "A\n",
      "C\n",
      "B\n"
     ]
    }
   ],
   "source": [
    "d = {'A': 10, 'C': 20, 'B': 30}\n",
    "d['C'] = 25\n",
    "for k in d:\n",
    "    print(k)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "61409c94-2334-4105-ab51-366090c232de",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "C\n",
      "B\n",
      "A\n"
     ]
    }
   ],
   "source": [
    "del d['A']\n",
    "d['A'] = 15\n",
    "\n",
    "for k in d:\n",
    "    print(k)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "968865f2-1162-4dc5-845c-a880d41c9cd1",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "20341bb0-bfc0-4310-a252-be7a7d5b9d6e",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "id": "f95e851d-9aaa-4795-9dc8-745e753ec0b4",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "10\n",
      "9\n",
      "8\n",
      "7\n",
      "6\n",
      "5\n",
      "4\n"
     ]
    }
   ],
   "source": [
    "for i in range(10, 3, -1):\n",
    "    print(i)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "0533449d-d1fa-4055-b2fa-302bf2976808",
   "metadata": {},
   "source": [
    "<br>\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "id": "b5d0ecfd-7309-49c8-a805-c8b14cbc2b13",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "A\n",
      "B\n",
      "C\n"
     ]
    }
   ],
   "source": [
    "s = ['A', 'B', 'C']\n",
    "for i in s:\n",
    "    print(i)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 34,
   "id": "45ffb65b-01cb-4d80-942a-7e94987b84da",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "0 A\n",
      "1 B\n",
      "2 C\n"
     ]
    }
   ],
   "source": [
    "s = ['A', 'B', 'C']\n",
    "for i in range(len(s)):\n",
    "    print(i, s[i])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 35,
   "id": "81cde64b-94d2-407a-9708-d360d696f046",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "0 A\n",
      "1 B\n",
      "2 C\n"
     ]
    }
   ],
   "source": [
    "for i, item in enumerate(s):\n",
    "    print(i, item)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 36,
   "id": "ba80189e-f7fe-4cb8-a727-1067450f9f26",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "(0, 'A')\n",
      "(1, 'B')\n",
      "(2, 'C')\n"
     ]
    }
   ],
   "source": [
    "for t in enumerate(s):\n",
    "    print(t)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "id": "b8c080bb-692a-4171-9cb1-5bfd5d77dc91",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "(0, 'A') 0 A\n",
      "(1, 'B') 1 B\n",
      "(2, 'C') 2 C\n"
     ]
    }
   ],
   "source": [
    "for t in enumerate(s):\n",
    "    i, item = t\n",
    "    print(t, i, item)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "id": "d543c035-c113-4ff2-a3dc-46167454edb9",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "201 张三\n",
      "202 李四\n",
      "203 王五\n"
     ]
    }
   ],
   "source": [
    "s = [(201, '张三'), (202, '李四'), (203, '王五')]\n",
    "for i in s:\n",
    "    print(i[0], i[1])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "id": "d4e5512a-9291-4c2f-a428-b805ab3fefa1",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "201 张三\n",
      "202 李四\n",
      "203 王五\n"
     ]
    }
   ],
   "source": [
    "s = [(201, '张三'), (202, '李四'), (203, '王五')]\n",
    "for 学号, 姓名 in s:\n",
    "    print(学号, 姓名)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "72d51eb7-e1af-4bed-b33e-cb3bb106f84e",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": 27,
   "id": "54a3305f-8ce1-472a-b447-f5958efd7501",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "10"
      ]
     },
     "execution_count": 27,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "count = 0\n",
    "for _ in range(10):\n",
    "    count += 1\n",
    "count"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "b3ea92f1-cab6-4b62-9437-cab5d9bda244",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "id": "f34dd5f0-2527-4c51-b7e8-9925a7b752a6",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "5050"
      ]
     },
     "execution_count": 13,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "s = 0\n",
    "for i in range(1, 101):\n",
    "    s += i\n",
    "s"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "id": "e876da01-8317-4e9b-8dd9-2a9f25c35e8a",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[0, 2, 4, 6, 8, 10, 12, 14, 16, 18]"
      ]
     },
     "execution_count": 19,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "s = []\n",
    "for i in range(0, 20, 2):\n",
    "    s.append(i)\n",
    "\n",
    "s"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "id": "df3251ff-dea3-4814-a8f7-aae821d92157",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "[0, 2, 4, 6, 8, 10, 12, 14, 16, 18]"
      ]
     },
     "execution_count": 18,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "list(range(0, 20, 2))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "id": "d0108ac0-59ea-4195-a5d3-9c8cc8592557",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "range(0, 20, 2)"
      ]
     },
     "execution_count": 21,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "range(0, 20, 2)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "0cd590e2-9d2e-46ae-9014-16491e2f2f2f",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "id": "e065e048-0272-40df-8515-624677505e06",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "1x1=1  \n",
      "2x1=2  2x2=4  \n",
      "3x1=3  3x2=6  3x3=9  \n",
      "4x1=4  4x2=8  4x3=12 4x4=16 \n",
      "5x1=5  5x2=10 5x3=15 5x4=20 5x5=25 \n",
      "6x1=6  6x2=12 6x3=18 6x4=24 6x5=30 6x6=36 \n",
      "7x1=7  7x2=14 7x3=21 7x4=28 7x5=35 7x6=42 7x7=49 \n",
      "8x1=8  8x2=16 8x3=24 8x4=32 8x5=40 8x6=48 8x7=56 8x8=64 \n",
      "9x1=9  9x2=18 9x3=27 9x4=36 9x5=45 9x6=54 9x7=63 9x8=72 9x9=81 \n"
     ]
    }
   ],
   "source": [
    "for i in range(1, 10):\n",
    "    for j in range(1, i + 1):\n",
    "        print(f'{i}x{j}={i*j:<2}', end=' ')\n",
    "    print()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "2cb4a2a3-0f8c-4a57-b297-289610500a7d",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "markdown",
   "id": "e657e2d2-0d76-44e6-884f-b46a961c45fe",
   "metadata": {},
   "source": [
    "## 循环控制语句\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "792d1f96-2ef7-4b6c-b3c7-6eaac9ba8d92",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "0\n",
      "1\n",
      "2\n"
     ]
    }
   ],
   "source": [
    "for i in range(5):\n",
    "    if i == 3:\n",
    "        break\n",
    "    print(i)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "503940e0-968a-46cc-833b-5fd3f17895f4",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "0\n",
      "1\n",
      "2\n",
      "4\n"
     ]
    }
   ],
   "source": [
    "for i in range(5):\n",
    "    if i == 3:\n",
    "        continue\n",
    "    print(i)"
   ]
  },
  {
   "cell_type": "markdown",
   "id": "5db00d13-b691-4660-ac9f-64aaaab1633a",
   "metadata": {},
   "source": [
    "## while语句"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 43,
   "id": "342e9097-b3bd-41bd-bc2e-e385b4c59cea",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "0\n",
      "1\n",
      "2\n",
      "3\n",
      "4\n"
     ]
    }
   ],
   "source": [
    "i = 0\n",
    "while i < 5:\n",
    "    print(i)\n",
    "    i += 1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "be83032e-dc2e-48d6-bc09-b8345521a338",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "markdown",
   "id": "3dd834e9-e68e-44ff-9d46-f85b245329cc",
   "metadata": {},
   "source": [
    "## 循环`else`从句"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "id": "2926e760-db2c-41c1-96b7-aebc720a4928",
   "metadata": {},
   "outputs": [],
   "source": [
    "s = [10, 20, 30, 40]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "id": "d64c38da-a75a-4c96-93e6-7ad84924667f",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "没有11\n"
     ]
    }
   ],
   "source": [
    "x = 11\n",
    "\n",
    "for i in s:\n",
    "    if i == x:\n",
    "        print(f'找到{x}')\n",
    "        break\n",
    "else:\n",
    "    print(f'没有{x}')"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "id": "67d5e65f-a14e-4666-9fbe-843066c65d32",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "找到20\n"
     ]
    }
   ],
   "source": [
    "x = 20\n",
    "\n",
    "for i in s:\n",
    "    if i == x:\n",
    "        print(f'找到{x}')\n",
    "        break\n",
    "else:\n",
    "    print(f'没有{x}')"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.11.4"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
